# author = "Ronald"
# date   = 24-04-2010 07:29:02 PM

from OpenGL.raw.GL.constants import GL_LEQUAL
from OpenGL.raw.GL.VERSION.GL_3_0 import GL_DEPTH_BUFFER
from OpenGL.raw.GL.VERSION.GL_3_0 import GL_RASTERIZER_DISCARD
from OpenGL.raw.GL.constants import GL_CULL_FACE
from OpenGL.GL import *
from OpenGL.GL import glScale
from OpenGL.GLU import *
from OpenGL.GLUT import *
from OpenGL.raw.GL import glDepthFunc
from leftArm import LeftArm
from numpy import *
from rightArm import RightArm
from body import Body
from leg import Leg
from robot import Robot
from truck import Truck
from surface import Surface
# import sys
# import math

windowId = -1
WIDTH = 800
HEIGHT = 600
DISTANCE = 5

lightamb  =  [1, 1, 1, 1]
lightdiff =  [1, 1, 1, 1]
lightspec =  [1, 1, 1, 1]
lightpos  =  [0, 0, 1, 0]

left = 0
right = 1

theta = 65.0
near = 1.0
far = 20.0

fovy   = 65.0
aspect = float(WIDTH) / float(HEIGHT)
znear  = 1
zfar   = 40
eye    = [0, 0, 5]
center = [0, 0, 0]
up     = [0, 1, 0]

#arm = Arm(0.5, 0, 0, 0, 0, 0)
leftArm = LeftArm(0.5, [-1.5, 1.5, 0])
rightArm = RightArm(0.5, [1.5, 1.5, 0])
body = Body(4, [0,0,0])
leg = Leg(1.5, [0, -0.56, 0])
robot = Robot(1, [0,0.5,-1.5])
truck = Truck(1, [0,0.5,-1.5])
surface = Surface(50,[0,-29,0])

# Configuramos OpenGL
def InitGL():

    glClearColor(0.0, 0.0, 0.0, 0.0)	# Color negro, sin transparencia

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glViewport(0, 0, WIDTH, HEIGHT)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluOrtho2D(-1, 1, -1, 1)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

def display():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    #glLoadIdentity()                # clear the matrix

#    rightArm.draw()
#    leftArm.draw()
#    body.draw()
#    leg.draw()
    robot.draw()
    #truck.draw()
    surface.draw()
    
    glFlush();

    glutSwapBuffers()

def myinit():
    glDepthFunc(GL_LEQUAL)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glEnable(GL_DEPTH_TEST)

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(fovy, aspect, znear, zfar)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    gluLookAt(eye[0], eye[1], eye[2], center[0], center[1], center[2], up[0], up[1], up[2])

#    glShadeModel (GL_FLAT)
#    glEnable(GL_LIGHTING)
#    glEnable(GL_LIGHT0)
#    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0.5,0.5,0.5,1])
#    glLightfv(GL_LIGHT0, GL_AMBIENT,  lightamb)
#    glLightfv(GL_LIGHT0, GL_DIFFUSE,  lightdiff)
#    glLightfv(GL_LIGHT0, GL_SPECULAR, lightspec)
#    glLightfv(GL_LIGHT0, GL_POSITION, lightpos)
#    glEnable(GL_COLOR_MATERIAL)
#    glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE)
#    glEnable(GL_TEXTURE_GEN_S);
#    glEnable(GL_TEXTURE_GEN_T);
#    glEnable(GL_TEXTURE_GEN_R);


#    glTranslatef(-VIEWPOINT_Y,-VIEWPOINT_Z,-VIEWPOINT_X)
#    ambientLight =array([0.1,0.1,0.1,1.0])
     #glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight)
#
#    diffuseLight =([0.8,0.8,0.8,1.0])
#    glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight)
#
#    specularLight =([0.5,0.5,0.5,1.0])
#    glLightfv(GL_LIGHT0,GL_SPECULAR,specularLight)
#
#    lightPos =([0.0,0.0,1.0,0.0])
#    glLightfv(GL_LIGHT0,GL_POSITION,lightPos)
#
##    specularReflection =([1.0,1.0,1.0,1.0])
##    glMaterialfv(GL_FRONT, GL_SPECULAR, specularReflection)
##    glMateriali(GL_FRONT,GL_SHININESS,128)
#
    glEnable(GL_LIGHT0)
    glEnable(GL_LIGHTING)
#    #glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight)
##    glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, 1.0)
    glEnable(GL_COLOR_MATERIAL)
    glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE)
    glEnable(GL_NORMALIZE)
    glEnable(GL_CULL_FACE)
##    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST)
##    glTranslatef(VIEWPOINT_Y,VIEWPOINT_Z,VIEWPOINT_X)

def myReshape(w, h):
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(fovy, aspect, znear, zfar)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    gluLookAt(eye[0], eye[1], eye[2], center[0], center[1], center[2], up[0], up[1], up[2])
#    glViewport(0, 0, w, h)
#    glMatrixMode(GL_PROJECTION)
#    glLoadIdentity()
#    gluPerspective(fovy, aspect, znear, zfar)
##    gluPerspective(theta, w / h, near, far)
##    gluLookAt(0, 0, DISTANCE, 0, 0, 0, 0, 1, 0)
#    gluLookAt(eye[0], eye[1], eye[2], center[0], center[1], center[2], up[0], up[1], up[2])
#    glMatrixMode(GL_MODELVIEW)
#    glLoadIdentity()
#    glTranslatef(0.0, 0.0, -5.0)

def keyPressed(*args):

    global ARMY
    global FOREARMY
    global DISTANCE
    global fovy, aspect, znear, zfar, eye, center, up

    key = args[0]

    if key == '\033':   # tecla escape
        glutDestroyWindow(windowId)
        sys.exit()

    #Teclas para mover al robot
    if key == 'g':
        robot.angleAdd()
    if key == 'h':
        robot.angleSubstract()
    if key == 't':
        robot.positionAdd(0.1)
    if key == 'y':
        robot.positionSubstract(0.1)

    # Teclas para mover el brazo derecho
    if key == 'e':
        robot.leftArm.armYSubstract()
    if key == 'd':
        robot.leftArm.armYAdd()
    if key == 'r':
        robot.leftArm.forearmYSubstract()
    if key == 'w':
	robot.leftArm.forearmYAdd()
    if key == 'f':
        robot.leftArm.armZAdd()
    if key == 's':
        robot.leftArm.armZSubstract()

    # Teclas para mover el brazo izquierdo
    if key == 'k':
        robot.rightArm.armYSubstract()
    if key == 'i':
        robot.rightArm.armYAdd()
    if key == 'o':
        robot.rightArm.forearmYSubstract()
    if key == 'u':
	robot.rightArm.forearmYAdd()
    if key == 'l':
        robot.rightArm.armZAdd()
    if key == 'j':
        robot.rightArm.armZSubstract()

    #teclas para mover la pierna izquierda
#    if key == '=':
#        robot.leftLeg.crusYAdd()
#    if key == '-':
#        robot.leftLeg.crusYSubstract()
#    if key == 'p':
#        robot.leftLeg.thighYAdd()
#    if key == ';':
#        robot.leftLeg.thighYSubstract()
#    if key == '[':
#        robot.leftLeg.thighZAdd()
#    if key == ']':
#        robot.leftLeg.thighZSubstract()

    #tecla para caminar
    if key == '/':
        robot.run()

    #tecla para caminar
    if key == '_':
        robot.moonwalk()

    #tecla para caminar
    if key == '!':
        robot.restorePosition()

    #disparo de bala
    if key == '.' :
        robot.rightArm.bulletBool = 1

#    if key == 'z':
#        znear+=1
#    elif key == 'x':
#        znear-=1
#    elif key == 'c':
#        zfar+=1
#    elif key == 'v':
#        zfar-=1

#    elif key == 'g':
#        fovy+=1
#    elif key == 'h':
#        fovy-=1

    elif key == 'z':
        eye[0]+=0.1
    elif key == 'x':
        eye[0]-=0.1
    elif key == 'c':
        eye[1]+=0.1
    elif key == 'v':
        eye[1]-=0.1
    elif key == 'b':
        eye[2]+=0.1
    elif key == 'n':
        eye[2]-=0.1

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(fovy, aspect, znear, zfar)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    gluLookAt(eye[0], eye[1], eye[2], center[0], center[1], center[2], up[0], up[1], up[2])


def main():
    global windowId

    # Configurar OpenGL
    glutInit(())

    # Mi modo de Display. Por ahora, no importa
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA)

    # Tamanio de la ventana.
    glutInitWindowSize(WIDTH, HEIGHT)

    # Donde queremos que se posicione la ventana al iniciar.
    glutInitWindowPosition(0, 0)
    windowId = glutCreateWindow("Proyecto Robot - Marcel_Ronald")

    # Defino la funci'on que se encargar'a de dibujar
    glutDisplayFunc(display)

    # La funci'on cuando el programa este Idle.
    # En un videoJuego, 'este ser'ia el lugar donde actualizo posiciones, calculo puntajes, etc.
    glutIdleFunc(display)

    # La funci'on a llamar cuando cambia el tamanio de la ventana.
    glutReshapeFunc (myReshape)

    # La funci'on que se encarga de recibir el input desde teclado
    glutKeyboardFunc (keyPressed)

    # Configuramos OpenGL para trabajar al tamanio deseado.
    myinit()

    # Loop infinito
    glutMainLoop()

main()


